/**
 * Created by shandianhaizan on 16/7/4.
 */



var AV = require('leanengine');
var validator = require('validator');

var model = {};

model.name = 'Customer';


model.getQuery = function(){
    return new AV.Query(model.name);
};



model.getObject = AV.Object.extend(model.name);



//Todo check param valid
model.validation = function(arg_customer){
    if(!arg_customer.username) {
        return {success:false, msg:"用户名错误"};
    }
    if(arg_customer.mobilePhoneNumber && !validator.isMobilePhone(arg_customer.mobilePhoneNumber, "zh-CN")){
        return {success:false, msg:"手机号格式错误"};
    }
    if(arg_customer.email && !validator.isEmail(arg_customer.email) ){
        return {success:false, msg:"邮箱格式错误"};
    }
    return {success:true};
};


model.formatQuery = function(arg_query, arg_params) {

    if(arg_params.name)  {
        arg_query.contains('username', arg_params.name);
    }
    if(arg_params.realName)  {
        arg_query.contains('realName', arg_params.realName);
    }
    if(arg_params.email)  {
        arg_query.contains('email', arg_params.email);
    }
    if(arg_params.group)  {
        arg_query.equalTo('group', arg_params.group);
    }
    if(arg_params.mobilePhoneNumber)  {
        arg_query.contains('mobilePhoneNumber', arg_params.mobilePhoneNumber);
    }
    if(arg_params.address)  {
        arg_query.contains('address', arg_params.address);
    }

    return arg_query;
};






module.exports = model;


